package org.example.judge.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.judge.service.TagService;
import org.example.judge_common.annotation.AuthAnnotation;
import org.example.judge_common.entity.ResponseResult;
import org.example.judge_pojo.Auth;
import org.example.judge_pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/tag")
public class TagController {
    @Qualifier("TagServiceImpl")
    @Autowired
    private TagService tagService;

    /**
     * 查询所有标签
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(){
        List<Tag> list = tagService.list();
        if(list==null||list.size()==0)return ResponseResult.NotData();
        return ResponseResult.success().setData(list);
    }
    @PostMapping("/save")
    @AuthAnnotation({Auth.ADMINISTRATOR})
    public ResponseResult save(@RequestBody Tag tag){
        tagService.save(tag);
        return ResponseResult.success();
    }
    @PutMapping("/update")
    @AuthAnnotation({Auth.ADMINISTRATOR})
    public ResponseResult updateById(@RequestBody Tag tag){
        tag.setUpdateTime(new Date());
        tagService.updateById(tag);
        return ResponseResult.success();
    }
    @DeleteMapping("/delete/{id}")
    @AuthAnnotation({Auth.ADMINISTRATOR})
    public ResponseResult deleteById(@PathVariable String id){
        tagService.removeById(id);
        return ResponseResult.success();
    }

}
